package com.sunrise.xdoc.entity.finance;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.sunrise.xdoc.entity.AbstractAuditEntity;
import com.sunrise.xdoc.entity.order.DonHang;

/**
 * Payment - Phieu thanh toan don hang, hoac phieu thu, phieu chi khac. Entity
 * nay dung de quan ly tai chinh.
 * 
 */
@Entity
@Table(name = "finance_phieu_thanh_toan")
@Inheritance(strategy = InheritanceType.JOINED)
public class PhieuThanhToan extends AbstractAuditEntity {
	private static final long serialVersionUID = 1L;
	// TODO Bang: this property should be moved out. 1 DonHang -> list
	// phieuThanhToan
	private DonHang donHang;
	private Long soTien = 0L;
	private String thongTin;

	// TODO Bang: có thể nhầm lẫn với nhân viên, nên đổi tên thành maOperator
	private String maNhanVien;
	private LoaiThanhToan loai;
	private Date ngayThanhToan;

	@Column(name = "ngay_thanh_toan")
	public Date getNgayThanhToan() {
		return ngayThanhToan;
	}

	public void setNgayThanhToan(Date ngayThanhToan) {
		this.ngayThanhToan = ngayThanhToan;
	}

	/*
	 * DON_HANG, PHIEU_THU: so tien > 0, PHIEU_CHI: so tien < 0
	 */
	public enum LoaiThanhToan {
		DON_HANG, PHIEU_THU, PHIEU_CHI
	}

	@ManyToOne
	@JoinColumn(name = "don_hang_id")
	public DonHang getDonHang() {
		return donHang;
	}

	public void setDonHang(DonHang donHang) {
		this.donHang = donHang;
	}

	@Column(name = "so_tien")
	@NotNull
	public Long getSoTien() {
		return soTien;
	}

	public void setSoTien(Long soTien) {
		this.soTien = soTien;
	}

	@Column(name = "thong_tin", length = 1024)
	public String getThongTin() {
		return thongTin;
	}

	public void setThongTin(String thongTin) {
		this.thongTin = thongTin;
	}

	@Column(name = "ma_nhan_vien")
	@NotNull
	public String getMaNhanVien() {
		return maNhanVien;
	}

	public void setMaNhanVien(String maNhanVien) {
		this.maNhanVien = maNhanVien;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "loai")
	@NotNull
	public LoaiThanhToan getLoai() {
		return loai;
	}

	public void setLoai(LoaiThanhToan loai) {
		this.loai = loai;
	}

	// TODO Bang: need annotation to validate the data @Valid
	public boolean validatePhieuThanhToan() {
		if (loai == LoaiThanhToan.PHIEU_CHI) {
			return soTien < 0;
		} else {
			return soTien > 0;
		}
	}
}
